package info.openlvb.openface.server.log;

import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.DateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Properties;

public class FaceLog {
	private static FaceLog instance = null;
	private FileInputStream in;
	private PrintWriter out;
	private String file;

	public String DEBUG = "DEBUG";
	public String INFO = "INFO";
	public String WARNING = "WARNING";
	private String modelog;

	public FaceLog(){
		Properties prop = new Properties();
		try {
			in = new FileInputStream("log.properties");
			prop.load(in);
			in.close();
			file = prop.getProperty("log.file");
			modelog = prop.getProperty("log.mode");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException ie){
			ie.printStackTrace();
		}
	}

	public final static FaceLog getInstance(){
		if(instance==null){
			instance = new FaceLog(); 
			return instance;
		}else{
			return instance;
		}
	}

	/**
	 * pour logger tous les messages : DEBUG, INFO et WARNING
	 * @param mode, message
	 * @author gbtux
	 */
	public void message(String mode, String message){
		try {
				out = new PrintWriter(new BufferedWriter(new FileWriter(file,true)));
				Date datecourante = new Date();
				Locale locale = Locale.getDefault();
				DateFormat dateformat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
				DateFormat timeformat = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
				//ici si le modelog est configuré en DEBUG on loggue tous les messages
				//qu'ils soient marqués DEBUG, INFO, ou WARNING
				if(modelog.equals(DEBUG)){
					out.println("DEBUG/"+mode+" : "+dateformat.format(datecourante)+"--"+timeformat.format(datecourante)+" "+message);
				}else{
					//sinon on affiche les message que si le mode configuré correspond
					//au mode du message
					if(modelog.equals(mode)){
						out.println(mode+" : "+dateformat.format(datecourante)+" "+message);
					}
				}
				out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	/**
	 * sert à logguer les stacktrace
	 */
	public static String printStackTrace(Exception e){
		if(e==null){
			return null;
		}else{
			StringWriter stringwriter = null;
			PrintWriter printwriter = null;
			try{
				stringwriter = new StringWriter();
				printwriter = new PrintWriter(stringwriter);
				e.printStackTrace(printwriter);
				return stringwriter.toString();
			}finally{
				flush(printwriter,stringwriter);
				close(printwriter,stringwriter);
			}
		}
	}

	private static void close(PrintWriter printwriter, StringWriter stringwriter) {
		printwriter.close();
		try {
			stringwriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void flush(PrintWriter printwriter, StringWriter stringwriter) {
		printwriter.flush();
		stringwriter.flush();
	}
}
